package com.elderly.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description 消息件的连接配置
 * @Date 2021/10/5 8:25
 * @Created 30500
 */
@Configuration
public class RabbitMQConfig {
    /**
     * 通配交换机
     */
    public static final String TOPIC_EXCHANGE = "topic_exchange";

    /**
     * 通配队列
     */
    public static final String TOPIC_QUEUE = "topic_queue";

    /**
     * @description 声明交换机
     * @author 30500
     * @date 2021/10/5 8:29
     * @type []
     * @return org.springframework.amqp.core.Exchange
     */
    @Bean("topicExchange")
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).durable(true).build();
    }

    /**
     * @description 声明队列
     * @author 30500
     * @date 2021/10/5 8:31
     * @type []
     * @return org.springframework.amqp.core.Queue
     */
    @Bean("topicQueue")
    public Queue topicQueue() {
        return QueueBuilder.durable(TOPIC_QUEUE).build();
    }

    /**
     * @description 绑定交换机和队列
     * @author 30500
     * @date 2021/10/5 8:32
     * @type [org.springframework.amqp.core.Queue, org.springframework.amqp.core.Exchange]
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding itemQueueExchange(@Qualifier("topicQueue") Queue queue,
                                     @Qualifier("topicExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("beordie.#").noargs();
    }
}
